local hudScreen = nil
local Alive = false
local Class = nil
local Team = 0
local WaitingToRespawn = false
local InRound = false
local RoundResult = 0
local RoundWinner = nil
local IsObserver = false
local ObserveMode = 0
local ObserveTarget = NULL
local InVote = false
local NextTip = 0
local CurrentTip = "lol"

local debug_disablecustomfonts = true

local Tips = { 
"With the All Weapons Have Scopes mod, you can use secondary fire (default right click) to zoom in with any weapon.", 
"Some weapons have a scope. Use Secondary Fire (default right click) to use them.",
"The green stripe below your ammo means that your active weapon has a scope function.",
"You cannot fire weapons while sprinting, unless the Attack While Sprinting mod is present.",
"With the Deadly Disease mod, all players periodically make an audible cough and take damage.",
"Knockback Bullets push your target. Use it to deal bonus falling damage to Super Jumping players!",
"The 50% Chance for No Damage mod makes bullets have a chance to bounce off their target, leaving your health to chance.",
"Be careful using Super Jump. Fall damage is a common problem, especially with the Knockback Bullets mod active.",
"The charge rate of weapons that have a charge function (like the Ion Charge Blaster) are affected by the Rate of Fire Increased/Decreased mods.",
"All players recieve the same weapons.",
"Strike Mods affect everybody. They're not individual perks, they change the game.",
"The crowbar is the only weapon both unaffected by most mods -and- can be used while sprinting, besides grenades and gravity guns."
}

if (file.Exists("resource/fonts/freeroad.ttf","GAME") or file.Exists("gamemodes/metastrike/content/resource/fonts/freeroad.ttf","GAME")) and !debug_disablecustomfonts then //Checks if the user actually has the font file

surface.CreateFont("MSHUDNumber", {
	font = "freeroad",
	size = 120,
	weight = 400
} )
surface.CreateFont("MSHUDNumber2", {
	font = "freeroad",
	size = 32,
	weight = 400
} )
surface.CreateFont("MSHUDNumber3", {
	font = "freeroad",
	size = 20,
	weight = 400
} )
surface.CreateFont("MSHUDNumber4", {
	font = "freeroad",
	size = 64,
	weight = 400
} )

else
surface.CreateFont("MSHUDNumber", {
	font = "coolvetica",
	size = 120,
	weight = 400
} )
surface.CreateFont("MSHUDNumber2", {
	font = "coolvetica",
	size = 32,
	weight = 400
} )
surface.CreateFont("MSHUDNumber3", {
	font = "coolvetica",
	size = 20,
	weight = 400
} )
surface.CreateFont("MSHUDNumber4", {
	font = "coolvetica",
	size = 64,
	weight = 400
} )
end
--surface.CreateFont("Counter-Strike", 32, 400, true, false, "MScstrike")
surface.CreateFont("MScstrike", {
	font = "Counter-Strike",
	size = 32,
	weight = 400
} )
surface.CreateFont("TabLarge", {
	font = "Arial",
	size = 20,
	weight = 400
} )
// Credit to Wizard of Ass for the rotated text code.
local sSetTextPos = surface.SetTextPos;
local sDrawText = surface.DrawText;
local cPushModelMatrix = cam.PushModelMatrix;
local cPopModelMatrix = cam.PopModelMatrix;
 
local mat = Matrix();
local matAng = Angle(0, 0, 0);
local matTrans = Vector(0, 0, 0);
local matScale = Vector(0, 0, 0);
local function drawSpecialText(txt, posX, posY, scaleX, scaleY, ang)
    matAng.y = ang;
    mat:SetAngles(matAng);
    matTrans.x = posX;
    matTrans.y = posY;
    mat:SetTranslation(matTrans);
    matScale.x = scaleX;
    matScale.y = scaleY;
    mat:Scale(matScale);
    sSetTextPos(0, 0);
    cPushModelMatrix(mat);
        sDrawText(txt);
    cPopModelMatrix();
end
// End Wizard of Ass code.

local function FormatTime(seconds)

	local m = seconds % 604800 % 86400 % 3600 / 60
	local s = seconds % 604800 % 86400 % 3600 % 60

	return math.floor(m), math.floor(s)
end

local function MSDeathNotices()
	local NewFeed = {}
	for k,v in pairs(NotifFeed) do
		if v.time>CurTime() then
			table.insert(NewFeed, v)
		end
	end
	NotifFeed = NewFeed
	local pad=10
	local spacing=46
	local boxcol = nil
	for k,v in pairs(NotifFeed) do
		surface.SetFont("MSHUDNumber3")
		if v.ntype=="PlayerKilledPlayer" then
			local atname = v.attacker
			local atcol = v.attacker_color
			local vcname = v.victim
			local vccol = v.victim_color
			local w1, h1 = surface.GetTextSize( atname )
			local w2, h2 = surface.GetTextSize( vcname )
			local boxw = w1+w2+64+(pad*2)
			local boxh = h1+(pad*2)
			if v.localplayer then
				boxcol = Color( 0, 100, 255, 200 ) else boxcol = Color( 0, 0, 0, 100 )
			end
			draw.RoundedBox( 8, ScrW()-boxw-pad, pad+((k-1)*spacing), boxw, boxh, boxcol )
			draw.SimpleTextOutlined( atname, "MSHUDNumber3", ScrW()-boxw, pad*2+((k-1)*spacing), atcol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			draw.SimpleTextOutlined( vcname, "MSHUDNumber3", ScrW()-boxw+w1+64, pad*2+((k-1)*spacing), vccol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			KIy = (64-h1)/2
			surface.SetTexture(surface.GetTextureID("VGUI/killicons/"..v.inflictor))
			surface.SetDrawColor( 255, 255, 255, 255 )
			surface.DrawTexturedRect( ScrW()-boxw+w1, pad*2+((k-1)*spacing)-KIy, 64, 64 )
		elseif v.ntype=="SpecialKilledPlayer" then
			local vcname = v.victim
			local vccol = v.victim_color
			atname=v.attacker
			atcol=Color(100,100,100,255)
			local w1, h1 = surface.GetTextSize( v.attacker )
			local w2, h2 = surface.GetTextSize( vcname )
			local boxw = w1+w2+64+(pad*2)
			local boxh = h1+(pad*2)
			if v.localplayer then
				boxcol = Color( 0, 100, 255, 200 ) else boxcol = Color( 0, 0, 0, 100 )
			end
			draw.RoundedBox( 8, ScrW()-boxw-pad, pad+((k-1)*spacing), boxw, boxh, boxcol )
			draw.SimpleTextOutlined( atname, "MSHUDNumber3", ScrW()-boxw, pad*2+((k-1)*spacing), atcol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			draw.SimpleTextOutlined( vcname, "MSHUDNumber3", ScrW()-boxw+w1+64, pad*2+((k-1)*spacing), vccol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			KIy = (64-h1)/2
			surface.SetTexture(surface.GetTextureID("VGUI/killicons/unknown"))
			surface.SetDrawColor( 255, 255, 255, 255 )
			surface.DrawTexturedRect( ScrW()-boxw+w1, pad*2+((k-1)*spacing)-KIy, 64, 64 )
		elseif v.ntype=="PlayerSpecialAction" then
			local vcname = v.nplayer
			local atcol = v.nplayer_color
			local w1, h1 = surface.GetTextSize( vcname.." " )
			local w2, h2 = surface.GetTextSize( v.action)
			local boxw = w1+w2+(pad*2)
			local boxh = h1+(pad*2)
			if v.localplayer then
				boxcol = Color( 0, 100, 255, 200 ) else boxcol = Color( 0, 0, 0, 100 )
			end
			draw.RoundedBox( 8, ScrW()-boxw-pad, pad+((k-1)*spacing), boxw, boxh, boxcol )
			draw.SimpleTextOutlined( vcname, "MSHUDNumber3", ScrW()-boxw, pad*2+((k-1)*spacing), atcol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			local vccol = Color( 255,255,255, 255 )
			draw.SimpleTextOutlined( v.action, "MSHUDNumber3", ScrW()-boxw+w1, pad*2+((k-1)*spacing), vccol, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
		end
	end
end

local function MSHUDPaint()
	CRoundState = GetGlobalInt("ms_rndstate", 0)
	CRoundTimer = GetGlobalFloat("ms_rndtimer", 0)
	CRoundResult = GetGlobalInt("ms_rndresult", 0)
	CMod1 = GetGlobalString("ms_mod1", "---")
	CMod2 = GetGlobalString("ms_mod2", "---")
	CMod3 = GetGlobalString("ms_mod3", "---")
	Alive = LocalPlayer():Alive()
	Team = LocalPlayer():Team()
	IsObserver = LocalPlayer():GetObserverMode()>0
	ObserveMode = LocalPlayer():GetObserverMode()
	ObserveTarget = LocalPlayer():GetObserverTarget()
	
	local pl = LocalPlayer()
	if Alive and Team ~= TEAM_CONNECTING and Team ~= TEAM_UNASSIGNED and Team ~= TEAM_SPECTATOR then
	if GAMEMODE:MSCheckForMod("No HUD") and CRoundState==2 and CurTime()>CRoundTimer then return end
	MSDeathNotices()
	//Armor
	if pl:Armor()>0 then
		surface.SetFont("MSHUDNumber4")
		surface.SetTexture(surface.GetTextureID("VGUI/metastrike_hud/armor"))
		surface.SetDrawColor( 255, 255, 255, 255 )
   		surface.DrawTexturedRect( 0, ScrH()-128, 256, 128 )
		surface.SetTextColor(255,255,255,255)
		drawSpecialText(tostring(pl:Armor()), 181, ScrH()-53, 0.5, 0.5, 348 )
	end
	
	// Health (goes on top of armor)
	surface.SetFont("MSHUDNumber")
	surface.SetTexture(surface.GetTextureID("VGUI/metastrike_hud/health"))
	surface.SetDrawColor( 255, 255, 255, 255 )
   	surface.DrawTexturedRect( 0, ScrH()-128, 256, 128 )
	surface.SetTextColor(255,255,255,255)
	drawSpecialText(tostring(pl:Health()), 49, ScrH()-90, 0.5, 0.5, 348 )
	
	// Ammo
	local currentweapon = LocalPlayer():GetActiveWeapon()
	if currentweapon~=NULL and string.find(currentweapon:GetClass(), "msw_") then
		if currentweapon.HasOwnScope or GAMEMODE:MSCheckForMod("All Guns Have Scopes") then
			surface.SetTexture(surface.GetTextureID("VGUI/metastrike_hud/ammo_scopeavailable"))
			surface.SetDrawColor( 255, 255, 255, 255 )
   			surface.DrawTexturedRect( ScrW()-256, ScrH()-128, 256, 128 )
		end
		surface.SetTexture(surface.GetTextureID("VGUI/metastrike_hud/ammo"))
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawTexturedRect( ScrW()-256, ScrH()-128, 256, 128 )
		if currentweapon.Primary.ClipSize>0 then --This ammo actually has a clip, right?
			surface.SetTextColor(255,255,255,255)
			surface.SetFont("MSHUDNumber")
			drawSpecialText(tostring(currentweapon:Clip1()).."/"..tostring(currentweapon:Ammo1()), ScrW()-155, ScrH()-110, 0.5, 0.5, 12 )
			surface.SetFont("MSHUDNumber2")
			draw.SimpleText(currentweapon.PrintName, "MSHUDNumber3", ScrW()-240,ScrH()-128, Color(255,255,255,255) )
		else --Special weapon
			surface.SetTextColor(255,255,255,255)
			surface.SetFont("MSHUDNumber")
			if currentweapon.Primary.Ammo~="none" then
				drawSpecialText("x"..tostring(currentweapon:Ammo1()), ScrW()-155, ScrH()-110, 0.5, 0.5, 12 )
			else
				drawSpecialText("SPECIAL", ScrW()-155, ScrH()-110, 0.5, 0.5, 12 )
			end
			surface.SetFont("MSHUDNumber2")
			draw.SimpleText(currentweapon.PrintName, "MSHUDNumber3", ScrW()-240,ScrH()-128, Color(255,255,255,255) )
		end
	end
	
	// Team Icon
	if pl:Team()==1 then titxt = surface.GetTextureID("VGUI/teamlogo_admin") else titxt = surface.GetTextureID("VGUI/teamlogo_mingebag") end
	surface.SetDrawColor(255,255,255,255)
	surface.SetTexture(titxt)
	surface.DrawTexturedRect(160,ScrH()-192,128,128)
	--[[local chtex = surface.GetTextureID("VGUI/crosshair_ms")
	surface.SetTexture( chtex )
   	surface.SetDrawColor( 255, 255, 255, 255 )
   	surface.DrawTexturedRect( ScrW() / 2 - 32, ScrH() / 2 - 32, 64, 64 )]]--
	if (currentweapon~=NULL and string.find(currentweapon:GetClass(), "msw_") and !currentweapon:GetNWBool("ScopeUsing", false)) or !(currentweapon~=NULL and string.find(currentweapon:GetClass(), "msw_")) then
		surface.SetDrawColor(team.GetColor(LocalPlayer():Team()))
		local chcone=0
		if IsValid(currentweapon) and string.find(currentweapon:GetClass(), "msw_") then
			chcone=currentweapon:GetModCone()
		else
			chcone=0.3
		end
		chpart = ScrH()*0.038
		chcone = chcone*10 * chpart
		chpart = chpart + (chcone/2)
		center = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
		center.x=math.floor(center.x)
		center.y=math.floor(center.y)
		surface.DrawLine(center.x-chcone-chpart,center.y,center.x-chcone,center.y)
		surface.DrawLine(center.x+chcone+chpart,center.y,center.x+chcone,center.y)
		surface.DrawLine(center.x,center.y-chcone-chpart,center.x,center.y-chcone)
		surface.DrawLine(center.x,center.y+chcone+chpart,center.x,center.y+chcone)
	end
	else
		MSDeathNotices()
		--[[local chtex = surface.GetTextureID("VGUI/crosshair_ms")
		surface.SetTexture( chtex )
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawTexturedRect( ScrW() / 2 - 32, ScrH() / 2 - 32, 64, 64 )]]--
		if CurTime() > NextTip then
			CurrentTip = table.Random(Tips)
			NextTip = CurTime() + 10
		end
		draw.SimpleTextOutlined( CurrentTip, "TabLarge", ScrW()/2, ScrH()-220, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP, 1, Color(0,0,0,255)  )
		if ( IsValid( ObserveTarget ) && ObserveTarget:IsPlayer() && ObserveTarget != LocalPlayer() && ObserveMode != OBS_MODE_ROAMING ) then
			if (pl:GetObserverMode() == OBS_MODE_FREEZECAM) then 
				draw.SimpleTextOutlined( "YOU WERE OVERRULED BY:", "MSHUDNumber3", ScrW()/2, ScrH()-220, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			else
				draw.SimpleTextOutlined( "SPECTATING:", "MSHUDNumber3", ScrW()/2, ScrH()-220, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			end
			draw.SimpleTextOutlined( string.upper(ObserveTarget:Nick()), "MSHUDNumber4", ScrW()/2, ScrH()-200, team.GetColor( ObserveTarget:Team() ), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			if ObserveTarget:Alive() and (ObserveTarget:Team()==pl:Team() or pl:Team()==TEAM_SPECTATOR) then
				draw.SimpleTextOutlined( "HP: "..ObserveTarget:Health(), "MSHUDNumber3", ScrW()/2, ScrH()-136, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
			end
		end
		
	end
	
	local tot_s = 0
	local befstr = "TIME REMAINING: "
	if ( CRoundTimer > CurTime() ) then 
		tot_s = math.floor(CRoundTimer-CurTime())
		befstr = "ROUND BEGINS IN "
	else 
		befstr = "TIME REMAINING: "
		tot_s = math.floor(60*cvars.Number( "ms_round_timer", 3 )+CRoundTimer-CurTime())
	end
	local m, s = FormatTime(tot_s)
	m_s = string.format("%02d", m) .. ":" .. string.format("%02d", s)
	if ( CRoundState==0 ) then
		draw.SimpleTextOutlined( "WAITING FOR PLAYERS", "MSHUDNumber3", ScrW()/2, 32, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )
	end
	if ( CRoundState==1 ) then
		draw.SimpleTextOutlined( "GAME BEGINS IN "..m_s, "MSHUDNumber3", ScrW()/2, 32, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )
	end
	if ( CRoundState==2 ) then
		draw.SimpleTextOutlined( befstr..m_s, "MSHUDNumber3", ScrW()/2, 32, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )
	end
	if ( CRoundState==3 ) then
		local txt = ""
		if CRoundResult==-1 then txt="DRAW GAME" end
		if CRoundResult==0 then txt="TIME OVER" end
		if CRoundResult==TEAM_MBAGS then txt="MINGEBAGS WIN" end
		if CRoundResult==TEAM_ADMIN then txt="ADMINISTRATORS WIN" end
		draw.SimpleTextOutlined( txt, "MSHUDNumber", ScrW()/2, 32, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 1, Color(0,0,0,255) )
	end
	
	--[[if #ents.FindByClass("metastrike_plantedc4")>0 then
		local m,s = FormatTime(GetGlobalInt("MSC4Countdown",0))
		
		draw.SimpleText( "d "..string.format("%02d", m)..":"..string.format("%02d", s),"MScstrike", 10, ScrH()-128, Color(255,255,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM)
	end]]--
	draw.SimpleTextOutlined( GAMEMODE:MSGetMod(1), "MSHUDNumber2", ScrW()/2, ScrH()-96, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )
	draw.SimpleTextOutlined( GAMEMODE:MSGetMod(2), "MSHUDNumber2", ScrW()/2, ScrH()-64, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )
	draw.SimpleTextOutlined( GAMEMODE:MSGetMod(3), "MSHUDNumber2", ScrW()/2, ScrH()-32, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255) )

	--GAMEMODE:HUDDrawPickupHistory()
end

function GM:HUDPaint()
	MSHUDPaint()
	GAMEMODE:HUDDrawTargetID()
	GAMEMODE:DrawDeathNotice( 0.85, 0.04 )
end

function GM:HUDPaintBackground()
	local currentweapon = LocalPlayer():GetActiveWeapon()
	if currentweapon~=NULL and string.find(currentweapon:GetClass(), "msw_") and currentweapon:GetNWBool("ScopeUsing", false) then
		surface.SetTexture( surface.GetTextureID("VGUI/ms_scope") )
   		surface.SetDrawColor( 255, 255, 255, 255 )
   		surface.DrawTexturedRect( ScrW()/2-ScrH()/2, 0, ScrH(), ScrH() )
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawRect( 0,0,(ScrW()-ScrH())/2,ScrH() )
		surface.DrawRect( ScrW()-(ScrW()-ScrH())/2,0,(ScrW()-ScrH())/2,ScrH() )
	end
end

local tohide = { -- This is a table where the keys are the HUD items to hide
	["CHudHealth"] = true,
	["CHudBattery"] = true,
	["CHudAmmo"] = true, 
	["CHudSecondaryAmmo"] = true
}
local function HUDShouldDraw(name) -- This is a local function because all functions should be local unless another file needs to run it
	if LocalPlayer() == NULL then return true end
	local currentweapon = LocalPlayer():GetActiveWeapon()
	if currentweapon == NULL then 
		if (tohide[name]) then return false end
	end
	if (tohide[name]) then     -- If the HUD name is a key in the table
		return false      -- Return false.
	end
end
hook.Add("HUDShouldDraw", "HideDefHUDStuff", HUDShouldDraw)